/*
 * 
 * Creator: mouzb
 */
package com.kingbi.util.jsf;

import java.util.List;

import javax.faces.component.UIData;
import javax.faces.component.UISelectBoolean;

/**
 * @author mouzb
 * 
 */
@SuppressWarnings("unchecked")
public class ListPageBean
{
    /*
     * 总页数
     */
    private int pageCount;

    /*
     * 总记录数
     */
    private long recordCount;

    /*
     * 页记录数
     */
    private int pageRecord = 10;

    /*
     * 当前页
     */
    private int currentPage;

    /*
     * 下一页
     */
    private int nextPage = 1;

    /*
     * 上一页
     */
    private int previousPage = 1;

    /*
     * 当前显示的开始记录数
     */
    private int startRecordNum = 0;

    /*
     * 当前显示的结束记录数
     */
    private int endRecordNum = 0;

    /*
     * 当前记录列表
     */
    private List items;

    /*
     * 用来标记修改和添加时更新查询，但要去除所有查询条件
     */
    public String saveSearchTag = "";

    /*
     * 标记为查询初始
     */
    public String searchTag = "";

    /*
     * 和当前列表组件捆绑的数据组件
     */

    public UIData tData;

    public UISelectBoolean selected = null;

    /*
     * 生成数的数据
     */
    public String dtree = "";

    /*
     * 当前功能 是否有管理权限 =SysMagContant.FUNCTION_MAG为有管理
     * =SysMagContant.FUNCTION_VIEW为只有浏览权限
     */
    private String magFlag = "";

    /**
     * 通过标识该变量来确认当前的查询是否更新，如果更新，应该重新统计total等数据
     */
    public boolean listUpdateFlag = false;

    public void dealPage(long recordCount, int pageRecord, int currentPage)
    {

        /*
         * 记录总数
         */
        this.recordCount = recordCount;
        /*
         * 每页记录数
         */
        this.pageRecord = pageRecord;

        /*
         * 总页数
         */
        if(recordCount % pageRecord == 0)
        {
            this.pageCount = (int) recordCount / pageRecord;
        }
        else
        {
            this.pageCount = (int) recordCount / pageRecord + 1;
        }

        /*
         * 当前页面号
         */
        if(currentPage > pageCount)
        {
            this.currentPage = pageCount;
        }
        else
        {
            this.currentPage = currentPage;
        }

        /*
         * 上一页
         */
        if(currentPage > 1)
        {
            this.previousPage = currentPage - 1;
        }
        else
        {
            this.previousPage = 1;
        }

        //System.out.println(nextPage + "  <>  " + pageCount + "  <>  " + currentPage);
        /*
         * 下一页 下一页小于最大页 或者 下一页等于最大页并且当前页为首页 那么 下一页等于当前页 + 1
         */
        if(nextPage < pageCount)
        {
            this.nextPage = currentPage + 1;
        }
        else if(nextPage >= pageCount && currentPage == 1)
        {
            this.nextPage = 2;
        }
        else if(nextPage >= pageCount && nextPage - currentPage == 2){
            this.nextPage = currentPage + 1;
        }
        else if(nextPage >= pageCount)
        {
            this.nextPage = pageCount;
        }

    }

    /**
     * 取得上一页 页号
     * 
     * @return
     */
    public void doPreviousPage()
    {
        if(currentPage > 1)
        {
            previousPage = currentPage - 1;
        }
        else
        {
            previousPage = -1;
        }
    }

    /**
     * 取得下一页 页号
     * 
     * @return
     */
    public void doNextPage()
    {
        if(nextPage < pageCount)
        {
            nextPage = currentPage + 1;
        }
        else
        {
            nextPage = -1;

        }
    }

    private boolean checked = false;

    public boolean isChecked()
    {
        return checked;
    }

    public void setChecked(boolean checked)
    {
        this.checked = checked;
    }

    public int getPageCount()
    {
        return pageCount;
    }

    public void setPageCount(int pageCount)
    {
        this.pageCount = pageCount;
    }

    public long getRecordCount()
    {
        return recordCount;
    }

    public void setRecordCount(long recordCount)
    {
        this.recordCount = recordCount;
    }

    public int getPageRecord()
    {
        return pageRecord;
    }

    public void setPageRecord(int pageRecord)
    {
        this.pageRecord = pageRecord;
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public int getNextPage()
    {
        return nextPage;
    }

    public void setNextPage(int nextPage)
    {
        this.nextPage = nextPage;
    }

    public int getPreviousPage()
    {
        return previousPage;
    }

    public void setPreviousPage(int previousPage)
    {
        this.previousPage = previousPage;
    }

    public List getItems()
    {
        return items;
    }

    public void setItems(List items)
    {
        this.items = items;
    }

    public boolean isListUpdateFlag()
    {
        return listUpdateFlag;
    }

    public void setListUpdateFlag(boolean listUpdateFlag)
    {
        this.listUpdateFlag = listUpdateFlag;
    }

    public UISelectBoolean getSelected()
    {
        return selected;
    }

    public void setSelected(UISelectBoolean selected)
    {
        this.selected = selected;
    }

    public UIData getTData()
    {
        return tData;
    }

    public void setTData(UIData data)
    {
        tData = data;
    }

    public String getSaveSearchTag()
    {
        return saveSearchTag;
    }

    public void setSaveSearchTag(String saveSearchTag)
    {
        this.saveSearchTag = saveSearchTag;
    }

    public String getSearchTag()
    {
        return searchTag;
    }

    public void setSearchTag(String searchTag)
    {
        this.searchTag = searchTag;
    }

    public String getDtree()
    {
        return dtree;
    }

    public void setDtree(String dtree)
    {
        this.dtree = dtree;
    }

    public String getMagFlag()
    {
        return magFlag;
    }

    public void setMagFlag(String magFlag)
    {
        this.magFlag = magFlag;
    }

    public int getStartRecordNum()
    {
        return startRecordNum;
    }

    public void setStartRecordNum(int startRecordNum)
    {
        this.startRecordNum = startRecordNum;
    }

    public int getEndRecordNum()
    {
        return endRecordNum;
    }

    public void setEndRecordNum(int endRecordNum)
    {
        this.endRecordNum = endRecordNum;
    }

}
